package com.yue.util;

/**
 * 功能描述: TODO <br/>
 * 创建时间: 2016/10/11<br/>
 *
 * @author QiuYang
 */
public class StringTools {

    /**
     * test whether ori contains a charter of tar
     * example: ori="abcde", tar="fgh", result:false
     *          ori="abcde", tar="egh", result:true
     * @param ori
     * @param tar
     * @return
     */
    public static boolean containsAny(String ori, String tar){
        char[] origins = ori.toCharArray();
        char[] targets = tar.toCharArray();

        for (char origin : origins) {
            for (char target : targets) {
                if (target == origin){
                    return true;
                }
            }
        }

        return false;
    }


    /**
     * example: str="abcdefghi", step=2, result={"ab", "cd", "ef", "gh", "i"}
     * @param str
     * @param step
     * @return
     */
    public static String[] split(String str, int step) {
        int length = (str.length() + step - 1) / step;
        String[] strs = new String[length];

        int start = 0, end = 0;
        for (int i = 0; i < length; i++) {
            start = end;
            end = (i + 1) * step;
            if (end > str.length()) {
                strs[i] = str.substring(start);
            } else {
                strs[i] = str.substring(start, end);
            }
        }

        return strs;
    }
}
